class Register{
   constructor(){
       this.init()
   }
   init(){
        let btn=document.querySelector('.submit_reg')
        // console.log(btn);

        btn.onclick=async ()=>{
            let phone=document.querySelector('#phone').value,
            password=document.querySelector('#password').value
            //电话号码的正则校验
            let reg1=/^1(3[0-9]|5[189]|8[6789])[0-9]{8}$/
            let res1=reg1.test(phone)
            let reg2=/\w{6,32}/
            let res2=reg2.test(password)
            // console.log(res1);
            if(!phone||!password||!res1||!res2){
                console.log(111);
               let warn1=document.querySelector('.warn1')
               this.animation(warn1)
            }else{
                
                let res=await axios.get('http://localhost:1998/user')
                let has=res.data.some(item=>{
                    return item.phone==phone
                })
                if(has){
                    let warn3=document.querySelector('.warn3')
                     this.animation(warn3)
                }else{
                    let data={}
                    data.phone=phone
                    data.id=phone
                    data.password=password
                    console.log(data);
                    await axios.post('http://localhost:1998/user',data)
                    data.carts=[]
                    await axios.post('http://localhost:1998/userinfo',data)
                    let warn2=document.querySelector('.warn2')
                    this.animation(warn2)
                    location.href="http://localhost:4023/html/login.html"
                }
            }
        }
   }
   //tips动画
   animation(ele){
       ele.style.display="block"
    ele.style.opacity=1
     let timer=setInterval(() => {
     if(ele.style.opacity==0){
        ele.style.display="none"
          clearInterval(timer)
    }else{
         ele.style.opacity-=0.02
   }  
   }, 10);
    }
   
}

new Register()